package com.fantabest.console.client.widget;

import com.extjs.gxt.ui.client.Registry;
import com.extjs.gxt.ui.client.data.BeanModel;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.FileUploadField;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.HiddenField;
import com.extjs.gxt.ui.client.widget.layout.ColumnData;
import com.extjs.gxt.ui.client.widget.layout.ColumnLayout;
import com.extjs.gxt.ui.client.widget.layout.FormLayout;
import com.fantabest.common.client.FantaBestConstants;
import com.fantabest.common.client.FantaBestMessages;
import com.fantabest.common.client.RegistryKeys;
import com.fantabest.common.client.widget.PlayersPanel;
import com.fantabest.common.entity.Season;

public class ImportPlayersPanel extends FormPanel {

	private FantaBestConstants constants = Registry.get(RegistryKeys.CONSTANTS);
	private FantaBestMessages messages = Registry.get(RegistryKeys.MESSAGES);

	private FileUploadField file;
	private HiddenField<Long> seasonId;
	private Button submitBt;

	public ImportPlayersPanel() {
		super();
		initUI();
	}

	private void initUI() {
		this.setHeaderVisible(false);
		this.setBodyBorder(false);
		this.setLabelWidth(120);
		this.setLabelAlign(LabelAlign.RIGHT);
		this.setEncoding(Encoding.MULTIPART);
		this.setMethod(Method.POST);
		this.setAction("common/import");

		LayoutContainer main = new LayoutContainer();
		main.setLayout(new ColumnLayout());

		LayoutContainer left = new LayoutContainer();
		left.setStyleAttribute("paddingRight", "10px");
		FormLayout layout = new FormLayout();
		layout.setLabelAlign(LabelAlign.RIGHT);
		left.setLayout(layout);
		// left.setBorders(true);

		seasonId = new HiddenField<Long>();
		seasonId.setName("seasonId");
		this.add(seasonId);
		
		file = new FileUploadField();
		file.setFieldLabel(constants.importFile());
		file.setName("playersFile");
		file.setAllowBlank(false);

		left.add(file);

		LayoutContainer right = new LayoutContainer();
		layout = new FormLayout();
		layout.setLabelAlign(LabelAlign.TOP);
		right.setLayout(layout);

		submitBt = new Button(constants.load());
		submitBt.addSelectionListener(new SelectionListener<ButtonEvent>() {
			@Override
			public void componentSelected(ButtonEvent ce) {
				submitForm();
			}
		});

		right.add(submitBt);
		ColumnData leftData = new ColumnData();
		leftData.setWidth(330);
		main.add(left, leftData);

		ColumnData rightData = new ColumnData();
		main.add(right, rightData);


		this.add(main);
		this.add(new PlayersPanel());

		main.layout(true);
	}


	private void submitForm() {
		BeanModel beanModel = Registry.get(RegistryKeys.SEASON_ENTITY);
		if(beanModel!=null)	{
			Season season = beanModel.getBean();
			seasonId.setValue(season.getId());
			this.submit();
		}	else	{
			MessageBox.info(constants.info(), messages.selectSeason(), null);
		}
		
	}
}
